package com.lc.cehui.ui.activity.home;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.glide.GlideApp;
import com.lc.cehui.http.request.CollectAddApi;
import com.lc.cehui.http.request.CollectDelApi;
import com.lc.cehui.http.request.CompanyInfoApi;
import com.lc.cehui.http.response.BaseBean;
import com.lc.cehui.http.response.CompanyInfoBean;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.ui.adapter.TabViewPagerAdapter;
import com.lc.cehui.ui.fragment.home.ParticularsInformationFragment;
import com.lc.cehui.ui.fragment.home.ParticularsPositionFragment;
import com.lc.cehui.util.SPUtil;
import com.lc.cehui.widget.SlidingTabLayout;
import com.lc.cehui.widget.XCollapsingToolbarLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 企业详情
 */
public class ParticularsActivity extends AppActivity implements XCollapsingToolbarLayout.OnScrimsListener{
    @BindView(R.id.rl_top)
    RelativeLayout rlTop;
    @BindView(R.id.img_shouchang)
    CheckBox checkbox;
    @BindView(R.id.img_logo)
    ImageView imgLogo;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_tag)
    TextView tvTag;
    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.slidingTabLayout)
    SlidingTabLayout slidingTabs;
    @BindView(R.id.viewpager)
    ViewPager vp;
    @BindView(R.id.tb_home_title)
    Toolbar tbHomeTitle;
    @BindView(R.id.ctl_home_bar)
    XCollapsingToolbarLayout mCollapsingToolbarLayout;
    private List<String> titles = new ArrayList<>();

    private List<Fragment> fragments = new ArrayList<>();
    private CompanyInfoBean data;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_particulars;
    }

    @Override
    protected void initView() {
        company_info();
        titles.add("公司信息");
        titles.add("在招职位");
        mCollapsingToolbarLayout.setOnScrimsListener(this);

        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    collect_add();
                } else {
                    collect_del();
                }
            }
        });


    }

    private void company_info() {
        EasyHttp.post(this)
                .api(new CompanyInfoApi().setCompany_id(getIntent().getStringExtra("id")).setUserid(SPUtil.get(this,IntentKey.USERID,0)))
                .request(new HttpCallback<String>(this) {

                    @Override
                    public void onSucceed(String datas) {
                        LogUtils.e(datas);
                        SPUtil.remove(ParticularsActivity.this, IntentKey.TEXT);
                        SPUtil.put(ParticularsActivity.this, IntentKey.TEXT, datas);
                        Gson gson = new Gson();
//                将存储的数据转成实体类
                        data = gson.fromJson(datas, new TypeToken<CompanyInfoBean>() {
                        }.getType());
                        GlideApp.with(ParticularsActivity.this).load(data.getData().getPicurl()).apply(new RequestOptions().placeholder(R.mipmap.logo_qyzp).error(R.mipmap.logo_qyzp)).into((ImageView)imgLogo);
                        GlideApp.with(ParticularsActivity.this).load(data.getData().getCompany_picurl()).apply(new RequestOptions().placeholder(R.mipmap.logo_qyzp).error(R.mipmap.logo_qyzp)).into((ImageView)img);
                        tvName.setText(data.getData().getName());
                        tvTag.setText(data.getData().getCity()+"  ｜  "+ data.getData().getDistrict()+"  ｜  "+ data.getData().getScale());
                        tvAddress.setText(data.getData().getProvince()+ data.getData().getCity()+ data.getData().getDistrict()+ data.getData().getCompany_address());
                        if (data.getData().getIs_collect() == 1) {
                            checkbox.setChecked(false);
                        } else {
                            checkbox.setChecked(true);
                        }
                        fragments.add(new ParticularsInformationFragment());
                        fragments.add(new ParticularsPositionFragment().newInstance(getIntent().getStringExtra("id")));

                        vp.setAdapter(new TabViewPagerAdapter(ParticularsActivity.this.getSupportFragmentManager(), ParticularsActivity.this,fragments,titles));
                        slidingTabs.setTabTitleTextSize(13);//标题字体大小
                        slidingTabs.setTitleTextColor(Color.parseColor("#028BFF"), Color.parseColor("#999999"));//标题字体颜色
                        slidingTabs.setTabStripWidth(60);//滑动条宽度
                        slidingTabs.setSelectedIndicatorColors(Color.parseColor("#028BFF"));//滑动条颜色
                        slidingTabs.setDistributeEvenly(true); //均匀平铺选项卡
                        slidingTabs.setViewPager(vp);//最后调用此方法
                        vp.setOffscreenPageLimit(fragments.size());//fragment保存的个数
                        vp.setCurrentItem(0);//设置卡在页面

                    }
                });
    }

    @Override
    protected void initData() {

    }
    private void collect_del() {
        EasyHttp.post(this)
                .api(new CollectDelApi().setUserid(SPUtil.get(this, IntentKey.USERID, 0)).setType(2).setStore_id(getIntent().getStringExtra("id")).setArticle_id("").setType_id("").setType_pid("").setCollect_id(""))
                .request(new HttpCallback<BaseBean>(this) {
                    @Override
                    public void onSucceed(BaseBean data) {

                        if (callBackCode(data)) {
                            toast("已取消收藏");
                        }
                    }
                });
    }

    private void collect_add() {
        EasyHttp.post(this)
                .api(new CollectAddApi().setUserid(SPUtil.get(this, IntentKey.USERID, 0)).setType(2).setArticle_id("").setType_id("").setType_pid("").setStore_id(getIntent().getStringExtra("id")))
                .request(new HttpCallback<BaseBean>(this) {
                    @Override
                    public void onSucceed(BaseBean data) {


                    }
                });

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.img_back, R.id.img_jubao, R.id.img_daohang})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.img_jubao:
                startActivity(ComplaintActivity.class);
                break;

            case R.id.img_daohang:
                break;
        }
    }
    @Override
    public void onScrimsStateChange(XCollapsingToolbarLayout layout, boolean shown) {
        if (shown) {
            tbHomeTitle.setBackgroundResource(R.color.main_color);
//
        } else {
            tbHomeTitle.setBackgroundResource(R.color.black100);

//
        }
    }
}
